﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;

namespace WebSocketClientApp
{
    public static class ControlExtensions
    {
        // 扩展方法，确保在UI线程上执行Action
        public static void SafeInvoke(this Control control, Action action)
        {
            if (control == null || control.IsDisposed)
                return;

            if (control.InvokeRequired)
            {
                try
                {
                    control.Invoke(action);
                }
                catch (ObjectDisposedException)
                {
                    // 控件可能已经被处置，这里可以选择忽略异常或进行其他处理
                }
            }
            else
            {
                action();
            }
        }
    }
}
